package cn.edu.bupt.scidata.controller;

import cn.edu.bupt.scidata.model.project.*;
import cn.edu.bupt.scidata.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping(value = "/all")
    public String index(@RequestParam(required = false, defaultValue = "1", name = "page") int page,
                        @RequestParam(required = false, defaultValue = "10", name = "size") int size,
                        @RequestParam(required = false, name = "code") String code,
                        @RequestParam(required = false, name = "time") String time,
                        @RequestParam(required = false, defaultValue = "relevance", name = "sort") String sort,
                        Map<String, Object> map) {
        //展示首页数据
        List<ElasticSearchProject> ret = new ArrayList<>();
        Page<ElasticSearchProject> results = projectService.fetchAll(size, page, sort);
        results.forEach(ret::add);
        long totalPage = results.getTotalPages();

        // 分页逻辑
        List<Integer> pageInfo = new ArrayList<>();
        for (int i = Math.max(1, page - 2); i <= Math.min(totalPage, page + 2); i++) {
            pageInfo.add(i);
        }

        long totalElements = results.getTotalElements();
        map.put("results", ret);
        map.put("page", page);
        map.put("pageInfo", pageInfo);
        map.put("firstPage", page == 1);
        map.put("lastPage", false);
        map.put("code", code);
        map.put("time", time);
        map.put("sort", sort);
        map.put("totalElements", totalElements);
        return "project/index";
    }

    @GetMapping(value = "/search")
    public String search(@RequestParam(required = false, name = "keyword", defaultValue = "") String keyword,
                         @RequestParam(required = false, defaultValue = "1", name = "page") int page,
                         @RequestParam(required = false, defaultValue = "10", name = "size") int size,
                         @RequestParam(required = false, name = "code") String code,
                         @RequestParam(required = false, name = "time") String time,
                         @RequestParam(required = false, defaultValue = "relevance", name = "sort") String sort,
                         Map<String, Object> map) {
        //展示首页数据
        List<ElasticSearchProject> results = new ArrayList<>();
        Page<ElasticSearchProject> ret = projectService.search(size, page, keyword, time, code, sort);
        ret.forEach(results::add);
        long totalPage = ret.getTotalPages();

        // 分页逻辑
        List<Integer> pageInfo = new ArrayList<>();
        for (int i = Math.max(1, page - 2); i <= Math.min(totalPage, page + 2); i++) {
            pageInfo.add(i);
        }

        long totalElements = ret.getTotalElements();
        map.put("results", results);
        map.put("page", page);
        map.put("pageInfo", pageInfo);
        map.put("firstPage", page == 1);
        map.put("lastPage", results.size() != size);
        map.put("code", code);
        map.put("time", time);
        map.put("sort", sort);
        map.put("keyword", keyword);
        map.put("totalElements", totalElements);
        return "project/index";
    }

    @GetMapping(value = "/detail")
    public String project(@RequestParam(name = "id") String approval, Map<String, Object> map) {
        MySQLProject project = projectService.getByApproval(approval);
        List<Results> results = projectService.getResultsByApproval(approval);
        map.put("project", project);
        map.put("results", results);
        return "project/project";
    }

    @GetMapping(value = "/trace/{id}")
    public ResponseEntity<Trace> trace(@PathVariable(name = "id") String id) {
        Trace ret = projectService.traceProjectByApproval(id);
        return ResponseEntity.ok(ret);
    }

    @GetMapping(value = "/trend/{id}")
    public ResponseEntity<Trend> trend(@PathVariable(name = "id") String id) {
        Trend ret = projectService.trendProjectByApproval(id);
        return ResponseEntity.ok(ret);
    }

    @GetMapping(value = "/researcher/projects/{id}")
    public ResponseEntity<Map<Integer, List<MySQLProject>>> researcher(@PathVariable(name = "id") String id) {
        Map<Integer, List<MySQLProject>> ret = projectService.getResearcherProjectsByApproval(id);
        return ResponseEntity.ok(ret);
    }
}
